#include "GameModuleState.h"
#include "MainLoopState.h"
#include "Portfolio.h"
#include <fstream>

GameModuleState::GameModuleState(string name) :ModuleState(name)
{
}

GameModuleState::~GameModuleState(void)
{
}

void GameModuleState::enter()
{
	//Reset Mouse Wheel
	position_mouse_z(0);

	//Generate Text for Game
	dataFileName = "PORT/" + name + "/" + name + "_G.dat";

	//Load Graphic file for the game
	port->getRMgr()->loadDataFile(dataFileName);

	//Read help text file
	helpFileName = "PORT/" + name + "/DOC/" + name + ".txt";
	char bufin[256];
	line = 0;
	fstream openf;
	openf.open(helpFileName.c_str(),ios::in);
	while (!openf.eof())
	{
		openf.getline(bufin, 256);
		strcpy(helpText[line++], bufin);
	}
	for(int i = 3; i < 256; i++)
	{
		helpText[0][i - 3] = helpText[0][i];
	}
	openf.close();
	openf.clear();

	//Copy the exe file descriptions
	exeFileName = "PORT/" + name + "/EXE.txt";
	openf.open(exeFileName.c_str(), ios::in);
	//release initial garbage
	openf.getline(exedesc[0], 256);
	while(!openf.eof())
	{
		openf.getline(exedesc[exenum], 256);
		openf.getline(exename[exenum++], 64);
	}
	openf.close();
	openf.clear();

	submenubutton_pmask = create_allegro_pmask(
		(BITMAP *)port->getRMgr()->GraphicPackA[Submenubuttonpmask].dat);

	//Mouse hit detection image
	B_mousehit = create_bitmap(1,1);
	mousehit_pmask = create_allegro_pmask(B_mousehit);
}

void GameModuleState::leave()
{
	destroy_pmask(mousehit_pmask);
	destroy_pmask(submenubutton_pmask);
	release_bitmap(B_mousehit);

	port->getRMgr()->unloadDataFile(dataFileName);
	clear_keybuf();

	port->getGUtil()->clearBuffers();
}

void GameModuleState::update(const float delta)
{
	checkMouse();
	if(state == 2)
	{
		for(int i = 0; i < exenum; i++)
		{
			if(mouse_x > 205 && mouse_x < 505) //test box length
			{
				if((mouse_y > 240 + i * 45) && (mouse_y < 295 + i * 45))
				{
					mouseover = i;
				}
			}
		}
		if(mouseover != -1 && mouselock == 0 && mouse_b == 1)
		{
			mouselock = 1;
			string exeDir = "PORT/" + name + "/EXE/";
			string cmdLine = "PORT/" + name + "/EXE/" + exename[mouseover];
			startproc(const_cast<char *>(cmdLine.c_str()), const_cast<char *>(exeDir.c_str()));
			mouseover = -1;
		}
		if(mouselock == 0 && mouse_b == 2)
		{
			mouselock = 1;
			state = 0;
		}
	}
	//quick exit
	if(mouse_b == 2 && mouselock == 0)
	{
		quit = 1;
	}


	// If we are quiting save the back buffer for the fade out
	if (quit)
	{
		port->changeState(new MainLoopState());
	}
}

void GameModuleState::render()
{
	// Begin drawing
	acquire_screen();

	port->getGUtil()->drawToBackBuffer(
		(BITMAP *)port->getRMgr()->GraphicPackA[Submenu].dat,
		0, 0);

	port->getGUtil()->drawToBackBuffer(
		(BITMAP *)port->getRMgr()->getData(1, dataFileName),
		50, 110);

	drawButtons();

	rect(port->getGUtil()->getLevel(0), 50, 110, 750, 510, C_ScrollBar);

	if(state == 1)
	{
		showInstructions();
	}

	//EXE MENU
	if(state == 2)
	{
		rectfill(
			port->getGUtil()->getLevel(0),
			100,200,
			100 + 600, 200 + 45 * exenum + 70,
			makecol(0,0,0));

		rectfill(
			port->getGUtil()->getLevel(0),
			105,205,
			105 + 600, 205 + 45 * exenum + 70,
			makecol(255,255,255));

		port->getGUtil()->drawTextToBackBuffer(
			port->getRMgr()->font,
			defaultmsg[1],
			105, 205,
			makecol(0,0,0));

		for(int i = 0; i < exenum; i++)
		{
			if(mouseover == i)
			{
				rectfill(port->getGUtil()->getLevel(0),
					205, 240 + i * 45,
					205 + 300, 240 + i * 45 + 40, 
					makecol(255,0,0));
				rectfill(port->getGUtil()->getLevel(0),
					207, 242 + i * 45,
					207 + 300, 242 + i * 45 + 40,
					makecol(128,128,128));
			}
			else
			{
				rectfill(port->getGUtil()->getLevel(0),
					205,240 + i * 45,
					205 + 300,240 + i * 45 + 40,
					makecol(0, 0, 0));
				rectfill(port->getGUtil()->getLevel(0),
					207,242 + i * 45,
					207 + 300, 242 + i * 45 + 40,
					makecol(255,255,255));
			}

			port->getGUtil()->drawTextToBackBuffer(
				port->getRMgr()->font,
				exedesc[i],
				207	,242 + i * 45,
				makecol(0,0,0));
		}
	}

	moduleTextHelp(buttonroll);

	//Mouse pointer dot
	rectfill(
		port->getGUtil()->getLevel(0),
		mouse_x - 3,
		mouse_y - 3,
		mouse_x + 3,
		mouse_y + 3,
		makecol(0,0,0));
	rectfill(
		port->getGUtil()->getLevel(0),
		mouse_x - 2,
		mouse_y - 2,
		mouse_x + 2,
		mouse_y + 2,
		makecol(255,255,255));

	port->getGUtil()->drawBackBufferToScreen();
	port->getGUtil()->clearBuffers();
	release_screen();
}

